/**
 * Copyright (c) 2009  Collaborative Development Group, C.S. Dept., University of Bari
 *
 * All rights reserved. This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0  which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 */
package it.uniba.di.cdg.jtalk.ui.login;

import static it.uniba.di.cdg.jtalk.ui.login.ILoginPresentationModel.PASSWORD_PROPERTY;
import static it.uniba.di.cdg.jtalk.ui.login.ILoginPresentationModel.USER_ID_PROPERTY;
import static org.osgi.service.log.LogService.LOG_DEBUG;
import static org.osgi.service.log.LogService.LOG_INFO;
import it.uniba.di.cdg.jtalk.ui.login.ILoginView.LogInButtonClicked;
import it.uniba.di.cdg.penelope.ui.annotation.Presenter;
import it.uniba.di.cdg.penelope.ui.job.RunAsJob;
import it.uniba.di.cdg.penelope.ui.mvp.AbstractPresenter;

import java.beans.PropertyChangeEvent;

import com.google.inject.Inject;

@Presenter( "login" )
public class LoginPresenter extends AbstractPresenter {

	private final ILoginView view;
	private final ILoginPresentationModel presentationModel;
	private final ILoginModel model;
	
	@Inject
	public LoginPresenter( ILoginView view, ILoginPresentationModel viewModel, ILoginModel model ) {
		super();
		this.view = view;
		this.presentationModel = viewModel;
		this.model = model;
	}
		
	@RunAsJob( "Logging in ..." )
	public void whenLogInButtonClicked( LogInButtonClicked event ) {
		model.log( LOG_INFO, "Start onLogIn()" );
		// Disable the view
		view.enableUserInput( false );

		try {
			model.logIn( 
					presentationModel.getUserId(), presentationModel.getPassword(), 
					presentationModel.isRememberPassword(), presentationModel.isAutomaticLogin() );
		} catch (Throwable ex) {
			// Display an error message.
			view.showError( new StringBuilder()
				.append( "An error has occurred when logging in:\n" )
				.append( ex.getMessage() )
				.toString() );
		} finally {
			view.enableUserInput( true );
		}
		model.log( LOG_INFO, "End onLogIn()" );
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.penelope.ui.mvp.AbstractPresenter#propertyChange(java.beans.PropertyChangeEvent)
	 */
	@Override
	public void propertyChange( PropertyChangeEvent evt ) {
		String propertyName = evt.getPropertyName();
		model.log( LOG_DEBUG, String.format( 
				"PropertyChanged( %s ) %s --> %s", 
				propertyName, evt.getOldValue(), evt.getNewValue() ) );
		
		if (propertyName.equals( USER_ID_PROPERTY ) || propertyName.equals( PASSWORD_PROPERTY )) {
			enableDisableLoginButton();
		}
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.penelope.ui.mvp.AbstractPresenter#onViewCreated()
	 */
	@Override
	public void onViewCreated() {
		super.onViewCreated();
		// Set initial state
		enableDisableLoginButton();
	}

	private void enableDisableLoginButton() {
		view.enableLoginButton( !presentationModel.getPassword().isEmpty() && !presentationModel.getUserId().isEmpty() );
	}
}
